package com.hxd.otc.module.my.presenter;

import android.support.annotation.NonNull;
import android.text.TextUtils;

import com.hxd.otc.R;
import com.hxd.otc.base.BaseBean;
import com.hxd.otc.common.UserHelper;
import com.hxd.otc.module.my.bean.AdvertCoinPriceBean;
import com.hxd.otc.module.my.bean.AdvertCoinTypeBean;
import com.hxd.otc.module.my.bean.AdvertPreBean;
import com.hxd.otc.module.my.bean.BankCardBean;
import com.hxd.otc.module.my.contract.PublishAdvertisementContract;
import com.hxd.otc.module.my.model.BankModel;
import com.hxd.otc.module.my.model.OtcAdvertModel;
import com.hxd.otc.mvp.base.BasePresenter;
import com.hxd.otc.net.BaseObserver;
import com.hxd.otc.utils.ResourceUtil;
import com.hxd.otc.utils.StringUtil;

import java.util.ArrayList;
import java.util.HashMap;

/**
 * 作  者 :  李宗源
 * 时  间 :  2018/10/18 14:18
 * 描  述 :  ${发布广告Presenter}
 */
public class PublishAdvertisementPresenter extends BasePresenter<PublishAdvertisementContract.View> implements PublishAdvertisementContract.Presenter {

    private final OtcAdvertModel mOtcAdvertModel;
    private final BankModel mBankModel;
    private ArrayList<String> mFiats = new ArrayList<>();//法币集合
    private ArrayList<String> mCoins = new ArrayList<>();//币种集合
    private ArrayList<String> mTrackingPlatForm = new ArrayList<>();
    private String mSelFiat;//当前选中的法币
    private String mSelCoin;//当前选中的币种
    private String mSelTrackingPlatForm;//当前追踪的浮动定价平台
    private HashMap<String, AdvertCoinTypeBean> mCoinTypeMaps = new HashMap<>();//币种符号-币种信息map
    private ArrayList<BankCardBean> mTempPaymentModes = new ArrayList<>();//支持当前法币的收款方式集合
    private HashMap<String, ArrayList<AdvertPreBean>> mAdvertPreBeanHashMap;//获取的前置信息
    private HashMap<String, AdvertPreBean> mCurTrackingPlatformMaps = new HashMap<>();//当前的追踪平台信息
    private HashMap<String, ArrayList<BankCardBean>> mTradeModesMaps = new HashMap<>();//法币符号-收款方式map
    private AdvertCoinPriceBean mAdvertCoinPriceBean;//当前币种价格限制Bean

    public PublishAdvertisementPresenter(PublishAdvertisementContract.View view) {
        super(view);
        mOtcAdvertModel = OtcAdvertModel.getInstance();
        mBankModel = BankModel.getInstance();
    }

    @Override
    public ArrayList<String> getFiats() {
        return mFiats;
    }

    @Override
    public String getSelFiat() {
        return StringUtil.emptyIfNull(mSelFiat);
    }

    @Override
    public ArrayList<String> getCoins() {
        return mCoins;
    }

    @Override
    public String getSelCoin() {
        return mSelCoin;
    }

    @Override
    public void setSelFiat(String fiat) {
        mSelFiat = fiat;

        //更新选中的交易对对应的单价限制
        AdvertCoinTypeBean selCoinTypeBeans = getSelCoinTypeBeans();
        ArrayList<AdvertCoinPriceBean> list = selCoinTypeBeans.getList();
        mAdvertCoinPriceBean = null;//还原配置
        if (list == null) {
            return;
        }
        for (AdvertCoinPriceBean advertCoinPriceBean : list) {
            //当法币相同时就是需要的单价限制对象
            if (mSelFiat.equals(advertCoinPriceBean.getCoinType())) {
                mAdvertCoinPriceBean = advertCoinPriceBean;
                return;
            }
        }
    }

    @Override
    public void setSelCoin(String coin) {
        mSelCoin = coin;

        //更新法币选择集合
        mFiats.clear();
        AdvertCoinTypeBean selCoinTypeBeans = getSelCoinTypeBeans();
        mFiats.addAll(selCoinTypeBeans.getUpportCurrencyArr());

        //更新选中法币，
        //当还没有选择法币，则默认选择法币列表中的第一个
        //当当前有选中的法币，但在当前法币列表中不存在，则也更新为当前法币列表中的第一个
        if (TextUtils.isEmpty(mSelFiat) || !mFiats.contains(mSelFiat)) {
            mView.updateFiat(mFiats.get(0));
        } else {
            mView.updateFiat(mSelFiat);
        }
    }

    @Override
    public void setCoinTypes(ArrayList<AdvertCoinTypeBean> coinTypeBeans) {
        mCoins.clear();
        mCoinTypeMaps.clear();
        for (AdvertCoinTypeBean advertCoinTypeBean : coinTypeBeans) {
            //当支持法币信息为空，则不应添加到可选列表中
            if (TextUtils.isEmpty(advertCoinTypeBean.getSupportCurrencyCode())) continue;
            mCoinTypeMaps.put(advertCoinTypeBean.getSymbol(), advertCoinTypeBean);
        }
        mCoins.addAll(mCoinTypeMaps.keySet());
    }

    /**
     * 根据法币整理收款方式
     *
     * @param bankCardBeans 收款方式信息
     */
    @Override
    public void setTradeModes(ArrayList<BankCardBean> bankCardBeans) {
        mTradeModesMaps.clear();
        for (BankCardBean bankCardBean : bankCardBeans) {
            for (String fiat : bankCardBean.getSupporCurrencyArray()) {
                ArrayList<BankCardBean> list;
                if (mTradeModesMaps.containsKey(fiat)) {
                    list = mTradeModesMaps.get(fiat);
                } else {
                    list = new ArrayList<>();
                }
                list.add(bankCardBean);
                mTradeModesMaps.put(fiat, list);
            }
        }
        mView.updatePaymentMode(mSelFiat);//通过更新法币以更新收款方式ui
    }

    /**
     * 获取支持对应法币的用户收款方式集合
     * @param fiat 法币
     * @return 对应法币的用户收款方式集合
     */
    @Override
    public ArrayList<BankCardBean> getTradeModesByFiat(String fiat) {
        return mTradeModesMaps.get(fiat);
    }

    @Override
    public AdvertCoinTypeBean getSelCoinTypeBeans() {
        return mCoinTypeMaps.get(mSelCoin);
    }

    /**
     * @return 当前币种价格限制Bean
     */
    @Override
    public AdvertCoinPriceBean getSelCoinPriceBean() {
        return mAdvertCoinPriceBean;
    }

    @Override
    public void publishAdvert(String singlePriceStr, String numsStr, final boolean isBuy, String supportBank, int minQuota, int maxQuota) {
        AdvertCoinTypeBean selCoinTypeBeans = getSelCoinTypeBeans();
        mOtcAdvertModel.publishAdvert(selCoinTypeBeans.getCoinId(), numsStr, singlePriceStr, selCoinTypeBeans.getSymbol(),
                isBuy ? 0 : 1, getSelFiat(), supportBank, selCoinTypeBeans.getMinVolume(), selCoinTypeBeans.getMaxVolume(),
                minQuota, maxQuota,
                new BaseObserver<String>() {
                    @Override
                    public void onNext(@NonNull String msg, @NonNull String s) {
                        mView.publishAdvertSuccess(isBuy);
                    }

                    @Override
                    public void onError(int errType, @NonNull String errMessage) {
                        mView.publishAdvertFail(errMessage);
                    }
                }, mView.<BaseBean<String>>bindAutoDispose());
    }
    @Override
    public void publishEasyAdvert(String singlePriceStr, String numsStr, final boolean isBuy, String supportBank, int minQuota, int maxQuota) {
        AdvertCoinTypeBean selCoinTypeBeans = getSelCoinTypeBeans();
        mOtcAdvertModel.publishEasyAdvert(selCoinTypeBeans.getCoinId(), numsStr, singlePriceStr, selCoinTypeBeans.getSymbol(),
                isBuy ? 0 : 1, getSelFiat(), supportBank, selCoinTypeBeans.getMinVolume(), selCoinTypeBeans.getMaxVolume(),
                minQuota, maxQuota,
                new BaseObserver<String>() {
                    @Override
                    public void onNext(@NonNull String msg, @NonNull String s) {
                        mView.publishAdvertSuccess(isBuy);
                    }

                    @Override
                    public void onError(int errType, @NonNull String errMessage) {
                        mView.publishAdvertFail(errMessage);
                    }
                }, mView.<BaseBean<String>>bindAutoDispose());
    }


    @Override
    public void publishFloatAdvert(String numsStr, final boolean isBuy, String supportBank,
                                   String floatPlat, double floatRate, String minPrice, int minQuota, int maxQuota) {
        AdvertCoinTypeBean selCoinTypeBeans = getSelCoinTypeBeans();
        mOtcAdvertModel.publishFloatAdvert(selCoinTypeBeans.getCoinId(), numsStr, selCoinTypeBeans.getSymbol(),
                isBuy ? 0 : 1, getSelFiat(), supportBank, selCoinTypeBeans.getMinVolume(), selCoinTypeBeans.getMaxVolume(),
                "1", floatPlat, floatRate, minPrice, minQuota, maxQuota, new BaseObserver<String>() {
                    @Override
                    public void onNext(@NonNull String msg, @NonNull String s) {
                        mView.publishAdvertSuccess(isBuy);
                    }

                    @Override
                    public void onError(int errType, @NonNull String errMessage) {
                        mView.publishAdvertFail(errMessage);
                    }
                }, mView.<BaseBean<String>>bindAutoDispose());
    }


    @Override
    public void requestBankCardList(final boolean showMessage) {
        mBankModel.getBankCardEasyList(new BaseObserver<ArrayList<BankCardBean>>() {
            @Override
            public void onNext(String msg, ArrayList<BankCardBean> bankCardBeans) {
                mView.requestBankCardListSuccess(bankCardBeans);
                if (showMessage)
                    mView.showToast(ResourceUtil.getString(R.string.get_fiat_info_success));
            }

            @Override
            public void onError(int errType, String errMessage) {
                if (showMessage)
                    mView.showToast(errMessage);
            }
        }, mView.<BaseBean<ArrayList<BankCardBean>>>bindAutoDispose());
    }

    @Override
    public void requestCoinTypeInfo(final boolean showErrMessage) {
        mOtcAdvertModel.getCoinType(new BaseObserver<ArrayList<AdvertCoinTypeBean>>() {
            @Override
            public void onNext(@NonNull String msg, @NonNull ArrayList<AdvertCoinTypeBean> coinTypeBeans) {
                mView.requestCoinTypeInfoSuccess(coinTypeBeans);
                if (showErrMessage)
                    mView.showToast(ResourceUtil.getString(R.string.get_coin_type_info_success));
            }

            @Override
            public void onError(int errType, @NonNull String errMessage) {
                if (showErrMessage)
                    mView.showToast(errMessage);
            }
        }, mView.<BaseBean<ArrayList<AdvertCoinTypeBean>>>bindAutoDispose(), UserHelper.getUserInfo().getId());
    }

    @Override
    public void requestPublishAdvertPre(final boolean showMessage) {
        mOtcAdvertModel.getPublishAdvertPre(new BaseObserver<HashMap<String, ArrayList<AdvertPreBean>>>() {
            @Override
            public void onNext(@NonNull String msg, @NonNull HashMap<String, ArrayList<AdvertPreBean>> advertPreBeanHashMap) {
                mAdvertPreBeanHashMap = advertPreBeanHashMap;
                mView.updateTrackingPlatformInfo();
                if (showMessage)
                    mView.showToast(ResourceUtil.getString(R.string.the_tracking_platform_information_was_obtained_successfully));
            }

            @Override
            public void onError(int errType, @NonNull String errMessage) {
                if (showMessage)
                    mView.showToast(errMessage);
            }
        }, mView.<BaseBean<HashMap<String, ArrayList<AdvertPreBean>>>>bindAutoDispose());
    }

    @Override
    public ArrayList<AdvertPreBean> getCurTrackingPlatformInfo() {

        if (mAdvertPreBeanHashMap == null) {//标识之前获取追踪平台信息失败，需要重新获取
            requestPublishAdvertPre(false);
            return new ArrayList<>();
        }

        String pair = getPair();
        if (TextUtils.isEmpty(pair)) return new ArrayList<>();

        if (!mAdvertPreBeanHashMap.containsKey(pair)) {
            return new ArrayList<>();
        }

        ArrayList<AdvertPreBean> advertPreBeans = mAdvertPreBeanHashMap.get(pair);

        return advertPreBeans == null ? new ArrayList<AdvertPreBean>() : advertPreBeans;
    }

    @Override
    public void updateTrackingPlatFormMaps(ArrayList<AdvertPreBean> curTrackingPlatformInfo) {
        mCurTrackingPlatformMaps.clear();
        mTrackingPlatForm.clear();
        if (curTrackingPlatformInfo.size() == 0) return;
        for (AdvertPreBean advertPreBean : curTrackingPlatformInfo) {
            mCurTrackingPlatformMaps.put(advertPreBean.getPlatName(), advertPreBean);
        }
        mTrackingPlatForm.addAll(mCurTrackingPlatformMaps.keySet());
    }

    @Override
    public ArrayList<String> getTrackingPlatForm() {
        return mTrackingPlatForm;
    }

    @Override
    public String getSelTrackingPlatForm() {
        return StringUtil.emptyIfNull(mSelTrackingPlatForm);
    }

    @Override
    public void setSelTrackingPlatForm(String selTrackingPlatForm) {
        mSelTrackingPlatForm = selTrackingPlatForm;
    }

    @Override
    public AdvertPreBean getCurAdvertPreBean() {
        return mCurTrackingPlatformMaps.get(mSelTrackingPlatForm);
    }

    /**
     * 获取交易对
     */
    private String getPair() {
        if (TextUtils.isEmpty(mSelCoin) || TextUtils.isEmpty(mSelFiat)) return "";
        return String.format("%s/%s", mSelCoin, mSelFiat);
    }

    /**
     * 缓存当前法币对应收款方式集合
     * @param cardBeans 当前法币支持的收款方式集合
     */
    @Override
    public void setTempPaymentMode(ArrayList<BankCardBean> cardBeans) {
        mTempPaymentModes.clear();
        mTempPaymentModes.addAll(cardBeans);
    }

    public ArrayList<BankCardBean> getTempPaymentModes() {
        return mTempPaymentModes;
    }
}